<?php

/*
 Plugin Name: Page Linker
 Plugin URI: http://code.google.com/p/wp-page-linker/
 Description: Links to pages from the rich text editor.
 Author: Tiago Pocinho, Siemens Networks, S.A.
 Version: 1.0 RC1
 */


/**
 * Thanks to Alex Rabe for a detailed explanation on how to add buttons to tinyMCE in wordpress 2.1
 * http://alexrabe.boelinger.com/?page_id=46
 */

// Needs to be loaded outside the class in order to work right
/*uncomment to implement quicktags using buttonsnap*/
//require_once('buttonsnap.php');

class wpPageLinker {
	/**
	 * Initializes the class
	 * Adds wordpress actions and filters      
	 **/
	function wpPageLinker_init(){

		global $wp_db_version;     
		// Check for WordPress 2.1+ and activated RTE  
		if ( 3664 <= $wp_db_version && 'true' == get_user_option('rich_editing') ) {    
			// add the button for wp21 in a new way  
			add_filter("mce_plugins", array('wpPageLinker',"wpPageLinker_button_plugin"), 0);
			// this plugin to tinyMCE: editor_plugin.js (wp2.1)
			add_action('tinymce_before_init',array('wpPageLinker',"wpPageLinker_button_script"));
			add_filter('mce_buttons', array('wpPageLinker',"wpPageLinker_button"), 0);
			
		}else { 
			/*uncomment to implement quicktags using buttonsnap*/ 
			/*
			$button_image_url = get_bloginfo("wpurl") . "/wp-content/plugins/wp-page-linker/button.gif";
			// Do it in the old way with buttonsnap  
			buttonsnap_separator();  
			buttonsnap_jsbutton($button_image_url, __('Select Pages', 'wpPageLinker'), 'wpPageLinker_buttonscript();');
			*/
		 
		}
		add_action('edit_page_form', array('wpPageLinker','insert_wpPageLinker_script'));
		add_action('edit_form_advanced', array('wpPageLinker','insert_wpPageLinker_script'));
		
	}
	
	// used to insert button in wordpress 2.1x editor
	/**
	 * Adds the button to the rich text editor recognized buttons list
	 * 
	 * @param array $buttons The buttons used in the tinyMCE editor
	 * @return array The buttons with the new button added to them
	 */
	function wpPageLinker_button($buttons) {
		$returnVal = array();
		$inserted =  false;
		//Try to place the plugin button next to the link button
		foreach($buttons as $button){
			array_push($returnVal, $button);
			if($button == "link"){
				array_push($returnVal, "wpPageLinker");
				$inserted = true;
			}
		}
		if(!$inserted){ //The link button was not found. Add to the end of the list
			array_push($buttons, "separator", "wpPageLinker");
		}
		return $returnVal;
	}
	
	/**
	 * Lets the rich text editor know that we have a plugin for it to use
	 *
	 * @param array $plugins Active tinyMCE plugins
	 * @return unknown
	 */
	function wpPageLinker_button_plugin($plugins) {
		//the -wpPageLinker indicates that it is an external plugin
		array_push($plugins, "-wpPageLinker");
		return $plugins;
	}
	
	/**
	 * 	Load the TinyMCE plugin : editor_plugin.js (wp2.1)
	 */
	function wpPageLinker_button_script() {
		$pluginURL = get_bloginfo("wpurl") . "/wp-content/plugins/wp-page-linker/";
		echo 'tinyMCE.loadPlugin("wpPageLinker", "'.$pluginURL.'");'."\n";
		return;
	}
	
	/**
	 * Load the Script for the Button(wp2.1)
	 *
	 */
	function insert_wpPageLinker_script() {
		$pluginURL = get_bloginfo("wpurl") . "/wp-content/plugins/wp-page-linker";
		echo "\n"." 
		<!-- wp-page-linker script-->
		<script type=\"text/javascript\">
			function wpPageLinker_buttonscript() { 
				tinyMCE.openWindow({
					file: '$pluginURL/selectPage.php',
					width: '400',
					height: '400'
				}, {
					resizable : \"no\", scrollbars : \"no\", 
					inline : \"yes\"
				})
			}
		</script>
		<!-- end of wp-page-linker -->";
		return;  
	}
}
//initializes the plugin
add_action('init', array('wpPageLinker','wpPageLinker_init'));
?>